//
//  YLPushViewModel.m
//  YLFinanceService
//
//  Created by she1323 on 2017/12/26.
//  Copyright © 2017年 she1323. All rights reserved.
//

#import "YLPushViewModel.h"
#import <YYModel.h>
#import "YLPushModel.h"
#import <UIKit/UIKit.h>
#import "YLProductViewController.h"
#import "YLRegularFinancingDetailController.h"
#import "YLCollectionDetailsViewController.h"
#import "YLWebViewController.h"
#import "WHWebViewController.h"
// 引入JPush功能所需头文件
#import <JPUSHService.h>
// iOS10注册APNs所需头文件
#ifdef NSFoundationVersionNumber_iOS_9_x_Max
#import <UserNotifications/UserNotifications.h>
#endif
#import "YLAccountTool.h"
@implementation YLPushViewModel

+ (void)pushWithInfo:(NSDictionary *)info
{
    YLPushModel *model = [YLPushModel yy_modelWithDictionary:info];
    
    if (model.RedirectType == 0) {
        return;
    }
    
    // 站内跳转
    if (model.RedirectType == 1) {
        if (model.RedirectId == 0) {
            [YLPushViewModel controllerWithIndex:0];
        }
        
        if (model.RedirectId == 1) {
            // 跳转到产品页
            YLProductViewController *p = (YLProductViewController *)[YLPushViewModel controllerWithIndex:1];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [p selectedIndex:0];
            });
            
            return;
        }
        
        if (model.RedirectId == 2) {
            // 跳转到产品页
            YLProductViewController *p = (YLProductViewController *)[YLPushViewModel controllerWithIndex:1];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [p selectedIndex:1];
            });
            
            return;
        }
        
        if (model.RedirectId == 3) {
            [YLPushViewModel controllerWithIndex:2];
            return;
        }
        
        if (model.RedirectId == 9) {
            // 合同详情页
            UITabBarController *t = (UITabBarController *)[UIApplication sharedApplication].keyWindow.rootViewController;
            YLRegularFinancingDetailController *con = [[YLRegularFinancingDetailController alloc] init];
            con.contractCode = model.RedirectKey;
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [(UINavigationController *)t.selectedViewController pushViewController:con animated:YES];
            });
            return;
        }
        
        if (model.RedirectId == 10) {
            // 收款详情
            UITabBarController *t = (UITabBarController *)[UIApplication sharedApplication].keyWindow.rootViewController;
            YLCollectionDetailsViewController *con = [[YLCollectionDetailsViewController alloc] init];
            con.contractCode = model.RedirectKey;
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [(UINavigationController *)t.selectedViewController pushViewController:con animated:YES];
            });
            return;
        }
        return;
    }
    
    if (model.RedirectType == 2) {
        // 外部链接
        WHWebViewController *webViewController = [[WHWebViewController alloc] init];
        webViewController.url = model.RedirectKey;
        UITabBarController *t = (UITabBarController *)[UIApplication sharedApplication].keyWindow.rootViewController;
        [(UINavigationController *)t.selectedViewController pushViewController:webViewController animated:YES];
        
    }
}

+ (UIViewController *)controllerWithIndex:(NSInteger)index
{
    UITabBarController *t = (UITabBarController *)[UIApplication sharedApplication].keyWindow.rootViewController;
    t.selectedIndex = index;

    UINavigationController *nav =  (UINavigationController *)t.viewControllers[1];
    [nav popToRootViewControllerAnimated:NO];
    
    return nav.viewControllers.firstObject;
}

+ (void)setNewTagsWithInfo:(NSDictionary *)info
{
    YLPushModel *model = [YLPushModel yy_modelWithDictionary:info];
    
    if (model.ContentType == 1 || model.ContentType == -1) {
        
        [JPUSHService cleanTags:^(NSInteger iResCode, NSSet *iTags, NSInteger seq) {
            
        } seq:1];
        
        [JPUSHService setTags:[NSSet setWithObject:model.Content] completion:^(NSInteger iResCode, NSSet *iTags, NSInteger seq) {
            
        } seq:1];
    }
   
}

+ (void)setUpMyPushInfo
{
    YLAccount *account = [YLAccountTool account];
    
    if (account) {
        [JPUSHService setAlias:account.alias completion:^(NSInteger iResCode, NSString *iAlias, NSInteger seq) {
            
        } seq:2];
        
        
        [JPUSHService setTags:[NSSet setWithObject:account.tag] completion:^(NSInteger iResCode, NSSet *iTags, NSInteger seq) {
            
        } seq:1];
    }
    
}

+ (void)clearPushInfo
{
    [JPUSHService cleanTags:^(NSInteger iResCode, NSSet *iTags, NSInteger seq) {
        
    } seq:1];
    [JPUSHService deleteAlias:^(NSInteger iResCode, NSString *iAlias, NSInteger seq) {
            
    } seq:2];
    
}

@end
